<?php
namespace app\back\controller;
class BaseAdmin extends \think\Controller{
    function _initialize()
    {
        error_reporting(E_ALL^E_NOTICE^E_WARNING);
        $this->assign("BACK_VIEW","/template/back/");
        $this->check_admin();
        $this->set_model();
    }

    public $model='';
    function error_tip($tip,$url=''){
        $str="<script>
            var url='{$url}';
            alert('{$tip}');
            if(url==false){
                history.go(-1);
            }else{
                location=url;
            }
            </script>";
        echo $str;
        exit();
    }

    public function  apiJosn($data=[],$code=200,$tip="成功返回"){

        header('Content-type: application/json');
        $tem_data['data']=$data;
        $tem_data['code']=$code;
        $tem_data['tip']=$tip;
        $arr=json_encode($tem_data);

        echo $arr;
        exit();

    }

    function success_tip($tip,$url=''){
        $str="<script>
            var url='{$url}';
            alert('{$tip}');
            if(url==false){
                history.go(-1);
            }else{
                location=url;
            }
            </script>";
        echo $str;
        exit();
    }

    function set_session_admin($admin_id,$name){
        session("admin_id",$admin_id);
        session("name",$name);

    }

    function get_admin_id(){
        return session('admin_id');
    }
    function get_admin_name(){
        return session("name");
    }

    function check_admin(){
        if($this->get_admin_id()){

        }else{
            return $this->error_tip("没有登陆",url("/back/Admin/index"));
        }
    }
    //插件图像上传
    public function uploadify()
    {
        // 1.获取上传的文件数据
        $file = request()->file("file");
        //var_dump($file);
        // 判断是否存在文件上传对象
        if ($file) {
            // 2.移动到指定目录下
            // ROOT_PATH 框架应用根目录
            // DS 当前系统的目录分隔符
            $files = $file->move(ROOT_PATH . "public" . DS . "uploads");
            //dump($files);
            if ($file != false) {
                //var_dump($files->getSaveName());
                $file_name = str_replace("\\", '/', $files->getSaveName());
                $data['img_url'] = "/uploads/" . $file_name;
                $data['img_url'] = pic_host($data['img_url']);
                return $this->apiJosn($data, 200);
            } else {
                return $this->apiJosn([], 201, '上传失败');
            }

            // 获取文件信息
            //dump($files->getSaveName());
            // 获取文件后缀名
            //dump($files->getExtension());

        } else {
            return $this->apiJosn([], 201, 'input中的name=file没有定义');
        }
    }

    function set_model(){
        $this->model='';
    }

    function get_model(){
        return $this->model;
    }

    function list(){
        $tem=$this->list_init();

        return $this->fetch("./common/list",$tem);
    }
    function list_init(){
        $model=$this->get_model();
        return [


                'list_title'=>['ID','名字'],
                'list_data_name'=>['id','name'],
                'list_data'=>$model->order("id","desc")->paginate(),
                'page_html'=>$model->paginate()->render(),
                'add_url'=>url('add')

        ];
    }

    function edit_init($id){
        $model=$this->get_model();
        $info=$model->where(['id'=>$id])->find();
        return [
            'form'=>[
                ['name'=>'id','value'=>$info->id,'type'=>'hidden','label_title'=>""],
                ['name'=>'name','value'=>$info->name,'type'=>'text','label_title'=>"名字"]
            ],
            'form_action'=>url("edit_act"),
        ];
    }
    function edit(){
        $params=request()->param();
        if($params['id']==false){
            return $this->error_tip('id不能为空');
        }
        $tem=$this->edit_init($params['id']);
        //var_dump($tem);
        return $this->fetch("./common/edit",$tem);
    }

    function edit_act(){
        //var_dump(44);exit();
        $params=request()->param();
        $model=$this->get_model();
        if($params['id']==false){
            return $this->error_tip('id不能为空');
        }
        $r=$model->where(['id'=>$params['id']])->update($params);
        if($r){
            return $this->success_tip("成功");
        }else{
            return $this->error_tip("操作失败");
        }
    }

    function add_init(){
        return [
            'form'=>[
                ['name'=>'id','value'=>'','type'=>'hidden','label_title'=>""],
                ['name'=>'name','value'=>'','type'=>'text','label_title'=>"名字"]
            ],
            'form_action'=>url("add_act"),
        ];
    }

    function add_act(){
        $params=request()->param();
        $model=$this->get_model();

        $r=$model->insert($params,false,true);;
        if($r){
            return $this->success_tip("成功");
        }else{
            return $this->error_tip("操作失败");
        }
    }

    function add(){
        $params=request()->param();

        $tem=$this->add_init();
        //var_dump($tem);
        return $this->fetch("./common/edit",$tem);
    }

    function del(){
        $params=request()->param();
        if($params['id']==false){
            return $this->error_tip("id不能为空");
        }
        $model=$this->get_model();
        $r=$model->where(['id'=>$params['id']])->delete();
        if($r){
            return $this->success_tip('删除成功');
        }else{
            return $this->error_tip('删除失败');
        }
    }


}